import BreadCrumbs from "./../../components/BreadCrumbs";
import "./style.scss";
import REST from "@module/Rest";
import Uts from "@module/Uts";
import usePartState from "@module/usePartState";
import EventApi from "@module/EventApi";
import ToastUtils from '@page/common/utils/ToastUtils'
import ZenUI from '@root/components/ZenUI/ZenUI'
import InfoModal from "@page/common/modal/InfoModal";
import urlHook from "@page/common/utils/urlHook";
import TopTable from './TopTable/TopTable'
import BottomSection from './BottomSection/BottomSection'


export default function BidDetailInfo(props) {

    const routerParams=$state.params||{}
    const unitId=routerParams.unitId
    const [stateObj, updateState] = usePartState({
        info:{},
        modalMsg:'放弃后您将不能继续参与此竞价报价，是否确认放弃竞价？'
    });
    let infoModalEle=null;
    const {info,modalMsg}=stateObj;
    const {unit,isEnd,bid}=info
    const {bidId,id}=bid||{}

    React.useEffect(() => {
        getItemAndBidInfo();
        EventApi.on('refreshBidInfo',handleRefreshEvent);

        return ()=>{
            EventApi.off('refreshBidInfo',handleRefreshEvent);
        }
    }, []);



    function handleRefreshEvent() {
        getItemAndBidInfo();
    }
    
    function getItemAndBidInfo() {
        let url=`/bid/anonymous/order/suppbid/getItemAndBidInfo`;
        let data=$state.params||{}
        let params= {
            id:data.id||0
        }
        let config={
            'SHOP-AUTHORIZATION':Uts.getCookie('loginToken')
        }
        ToastUtils.loading()
        REST.post(url,params,config)
            .then(res=>{
                console.log(res)
                ToastUtils.hide()
                if(res.resultCode=='3000'){
                    ZenUI.ST(res.msg,2)
                }
                if(res.resultCode='0000'){
                    updateState({
                        info:res
                    })
                }
            })
            .catch(err=>{
                ToastUtils.hide()
                console.log(err)
            })
    }



    return (
        <div className="zen_flex rj_bidlist-wp">
            <div className="zen_w-1200">
                <BreadCrumbs tittleList={[{ name: "在线竞价",canClick:true,path:'/bidList' }, { name:isEnd? '已结束':'竞价中' },{name:unit||''},{name:'竞价详情',canClick:true,path:`/bidDetail`,params:{id:unitId}},{name:'商品详情'}]} />
                <div className={'bid-info-detail'}>
                    <span className={'bid-info-detail-line'}></span>
                    <span className={'bid-info-title'}>商品信息</span>
                </div>
                <TopTable
                    info={info}
                />

                <div className={'bid-info-detail2'}>
                    <span className={'bid-info-detail-line'}></span>
                    <span className={'bid-info-title'}>报价信息</span>
                </div>
                <BottomSection
                    info={info}
                    giveupBid={()=>{
                        infoModalEle.showModal()
                    }}
                />
                <div className={'zen_h-130'}/>
                <InfoModal
                    className={'bid-inputbid-give-modal'}
                    content={modalMsg}
                    getRef={v => (infoModalEle = v)}
                    type={'choose'}
                    onConfrim={giveUp}
                />
            </div>
        </div>
    );
    

    //放弃竞价
    function giveUp() {
        let url=`/bid/order/suppbid/updateBidStatus`;

        let params={
            id: bidId||id,
            isGiveUp: true
        };

        REST.post(url,params)
            .then(res=>{
                if(res.resultCode == "0000") {
                    location.reload();
                }
            })
            .catch(err=>{
                console.log(err);
            })

    }

}





